package com.hnhy.hnhyscms_ws.controller;

import com.hnhy.hnhyscms_ws.dao.ISpstorageDao;
import com.hnhy.hnhyscms_ws.entity.Spoutsto;
import com.hnhy.hnhyscms_ws.entity.Spstorage;
import com.hnhy.hnhyscms_ws.service.ISpoutstoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/spoutsto")
public class SpoutstoController {
    @Autowired
    private ISpoutstoService iSpoutstoService;
    @Autowired
    private ISpstorageDao iSpstoragService;
    @RequestMapping(value = "/addSpoutsto",method= RequestMethod.POST)
    @ResponseBody
    public synchronized String addSpoutsto(@RequestParam String productNo, @RequestParam Float amount, @RequestParam String thirdOrderNo){
        Spstorage spstorage=iSpstoragService.getByProductNo(productNo);
        if(spstorage.getAmount()-amount<0)
            return "1";
        Spoutsto spoutsto=new Spoutsto();
        spoutsto.setAmount(amount);
        spoutsto.setSupplierName(spstorage.getSupplierName());
        spoutsto.setSupplierNo(spstorage.getSupplierNo());
        spoutsto.setBand(spstorage.getBand());
        spoutsto.setPrice(spstorage.getPrice());
        spoutsto.setProductName(spstorage.getProductName());
        spoutsto.setProductNo(spstorage.getProductNo());
        spoutsto.setSpec(spstorage.getSpec());
        spoutsto.setThirdOrderNo(thirdOrderNo);
        spoutsto.setUnit(spstorage.getUnit());
        spoutsto.setTotal(amount*spstorage.getPrice());
        iSpoutstoService.addSpoutsto(spoutsto);
        spstorage.setAmount(spstorage.getAmount()-amount);
        iSpstoragService.updateByProductNo(spstorage);
        return "0";

    }
}
